package com.mmy.controller.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.oa.RewardInfo;
import com.mmy.domain.oa.RewardType;
import com.mmy.domain.oa.VSysRewardInfo;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.oa.impl.RewardInfoServiceImpl;
import com.mmy.service.oa.impl.RewardTypeServiceImpl;

/**
 * 类的介绍：激励记录
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class RewardInfoController {

	@Autowired
	RewardInfoServiceImpl rewardInfoServiceImpl;

	@Autowired
	RewardTypeServiceImpl rewardTypeServiceImpl;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/rewardinfo")
	public String ToRewardInfo(Model model) {
		try {

			List<VSysRewardInfo> lstRewardInfo = rewardInfoServiceImpl.findAll();
			model.addAttribute("llist", lstRewardInfo);
			List<RewardType> lstRewardType = rewardTypeServiceImpl.findAll();
			model.addAttribute("ltlist", lstRewardType);
			return "OA/reward/rewardinfo";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/createrewardinfo")
	public String ToCreateRewardInfo(Model model) {
		List<RewardType> lstRewardType = rewardTypeServiceImpl.findAll();
		model.addAttribute("ltlist", lstRewardType);
		return "OA/reward/createrewardinfo";
	}

	/**
	 * 函数功能说明： 新建激励记录
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/subcreaterewardinfo")
	public String CreateRewardInfo(RewardInfo rewardInfo, HttpSession session) throws Exception {

		try {

			// 获取最大值
			Object oRewardID = rewardInfoServiceImpl.getMaxRewardID();
			/* 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一 */
			if (oRewardID == null) {
				rewardInfo.setRewardID(1);

			} else {
				rewardInfo.setRewardID((int) oRewardID + 1);
			}
			rewardInfo.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			rewardInfo.setCreator(tSysUser.getUserName());
			rewardInfoServiceImpl.save(rewardInfo.getRewardID());
			rewardInfoServiceImpl.save(rewardInfo);

			return "redirect:rewardinfo";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除激励记录
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@PostMapping("/delrewardinfo")
	@ResponseBody
	public String DelRewardInfo(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstRewardID = new ArrayList<>();

		for (String str : checklist) {
			lstRewardID.add(Integer.parseInt(str));
		}

		rewardInfoServiceImpl.delRewardInfo(lstRewardID);

		return "OA/reward/rewardinfo";
	}

	/**
	 * 函数功能说明： 修改激励记录
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdaterewardinfo")
	public String EnterUpdateRewardInfo(HttpServletRequest request, Model model) {

		int iRewardID = Integer.parseInt(request.getParameter("iRewardID"));

		RewardInfo rewardInfo = rewardInfoServiceImpl.findByRewardID(iRewardID);
		model.addAttribute("rewardInfo", rewardInfo);
		List<RewardType> lstRewardType = rewardTypeServiceImpl.findAll();
		model.addAttribute("ltlist", lstRewardType);
		return "OA/reward/updaterewardinfo";

	}

	@RequestMapping(value = "/updaterewardinfo")
	public String UpdateRewardInfo(RewardInfo rewardInfo, HttpSession session) throws Exception {

		try {

			/* 定义一个新的实体类对象 */
			RewardInfo drewardInfo = rewardInfoServiceImpl.findByRewardID(rewardInfo.getRewardID());
			drewardInfo.setTypeID(rewardInfo.getTypeID());
			drewardInfo.setReason(rewardInfo.getReason());
			drewardInfo.setMemo(rewardInfo.getMemo());
			drewardInfo.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			drewardInfo.setReviser(tSysUser.getUserName());
			rewardInfoServiceImpl.save(drewardInfo);
			return "redirect:rewardinfo";

		} catch (Exception e) {

		}
		return null;
	}

	@RequestMapping(value = "/entercheckupdaterewardinfo")
	public String EnterCheckUpdateRewardInfo(HttpServletRequest request, Model model) {

		int iRewardID = Integer.parseInt(request.getParameter("iRewardID"));

		RewardInfo rewardInfo = rewardInfoServiceImpl.findByRewardID(iRewardID);
		model.addAttribute("rewardInfo", rewardInfo);
		String strTypeName = rewardTypeServiceImpl.findByTypeID(rewardInfo.getTypeID()).getTypeName();
		model.addAttribute("strTypeName", strTypeName);
		return "OA/reward/checkupdaterewardinfo";

	}

	@RequestMapping(value = "/checkupdaterewardinfo")
	public String CheckUpdateRewardInfo(HttpServletRequest request, RewardInfo rewardInfo) throws Exception {

		try {

			/* 定义一个新的实体类对象 */
			RewardInfo drewardInfo = rewardInfoServiceImpl.findByRewardID(rewardInfo.getRewardID());
			drewardInfo.setTypeID(rewardInfo.getTypeID());
			drewardInfo.setReason(rewardInfo.getReason());
			drewardInfo.setMemo(rewardInfo.getMemo());
			drewardInfo.setMessage(rewardInfo.getMessage());
			drewardInfo.setConfirm(new Date());
			rewardInfoServiceImpl.save(drewardInfo);
			return "redirect:rewardinfo";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询激励记录信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/searchrewardinfo")
	public String FindRewardInfo(HttpServletRequest request, Model model) {

		String strTypeName = request.getParameter("TypeName");
		if (strTypeName == "") {
			List<VSysRewardInfo> lstRewardInfo = rewardInfoServiceImpl.findAll();
			model.addAttribute("llist", lstRewardInfo);
			List<RewardType> lstRewardType = rewardTypeServiceImpl.findAll();
			model.addAttribute("ltlist", lstRewardType);
			return "OA/reward/rewardinfo";
		} else {
			List<VSysRewardInfo> lstRewardInfo = rewardInfoServiceImpl.findByTypeName(strTypeName);
			model.addAttribute("llist", lstRewardInfo);
			List<RewardType> lstRewardType = rewardTypeServiceImpl.findAll();
			model.addAttribute("ltlist", lstRewardType);
			return "OA/reward/rewardinfo";
		}

	}

}
